clear
clear matrix
clear mata
set more off, perm
set matsize 11000
set maxvar 30000
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/
global output /Users/zachbrown/Projects/PriceTransparency/Output



////////////////////
// BY VISIT
////////////////////

// open visit dataset
use clean/radiology_visit_3.dta, clear

egen prov_proc_fe = group(proc_id_radio prov_id payer_id)

xi i.charlindex i.std_product_type i.payer_id

bys prov_id proc_id_radio: egen count_prov = count(from_date)

foreach var of varlist amt* {
	gen n_`var' = `var' if count_prov>=5
}

// Calculate interquartile range
foreach var of varlist amt* n_* {
	bys proc_id_radio visit_on_web ym: egen iqr_`var'=iqr(`var')
}

gen n_visit = 1
gen month = month(from_date)
collapse (sum) n_visit ///
	(sd) amt* ///
	(mean) age male zip_mean_income zip_pct_ba charlindex _I* ///
	(firstnm) iqr_* ///
	, by(proc_id_radio visit_on_web ym month year)
	
gsave clean/radiology_varyr_1.dta, replace



////////////////////
// BY PROVIDER
////////////////////


// open visit dataset
use clean/radiology_visit_3.dta, clear

egen prov_proc_fe = group(proc_id_radio prov_id payer_id)

// Make control dummies
xi i.charlindex i.std_product_type i.payer_id

bys prov_id proc_id_radio: egen count_prov = count(from_date)

gen n_visit = 1
gen month = month(from_date)

foreach var of varlist amt* {
	gen n_`var' = `var' if count_prov>=5
}

collapse (sum) n_visit ///
	(mean) amt* n_*  ///
	age male zip_mean_income zip_pct_ba charlindex _I* ///
	, by(proc_id_radio prov_id visit_on_web ym month year)
count

// Calculate interquartile range
foreach var of varlist amt* n_* {
	bys proc_id_radio visit_on_web ym: egen iqr_`var'=iqr(`var')
}

collapse (count) n_prov=n_visit (sum) n_visit ///
	(sd) amt* n_*  ///
	(mean) age male zip_mean_income zip_pct_ba charlindex _I* ///
	(firstnm) iqr_* ///
	, by(proc_id_radio visit_on_web ym month year)
	
gsave clean/radiology_varyr_2.dta, replace


////////////////////
// ANALYSIS
////////////////////



guse clean/radiology_varyr_1.dta, clear
gen post = (ym>=ym(2007,3))
gen web_post = (visit_on_web==1 & post==1)
label var web_post "$\text{OnWeb}\textsubscript{m}\times\text{Post}\textsubscript{t}$"
global ctr_vars1 = "age male zip_mean_income zip_pct_ba _Ichar*"


// Cost variance, allowed amount for visit
areg iqr_n_amt_allowed web_post i.ym $ctr_vars1, absorb(proc_id_radio) vce(cl ym)
summ iqr_n_amt_allowed if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a1

// Cost variance, allowed amount for procedure
areg iqr_n_amt_allowed_radio web_post i.ym $ctr_vars1, absorb(proc_id_radio) vce(cl ym)
summ iqr_n_amt_allowed_radio if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a2


guse clean/radiology_varyr_2.dta, clear
gen post = (ym>=ym(2007,3))
gen web_post = (visit_on_web==1 & post==1)
label var web_post "$\text{OnWeb}\textsubscript{m}\times\text{Post}\textsubscript{t}$"
global ctr_vars1 = "age male zip_mean_income zip_pct_ba _Ichar*"


// Provider price variance, allowed amount for visit
areg iqr_n_amt_allowed web_post i.ym $ctr_vars1, absorb(proc_id_radio) vce(cl ym)
summ iqr_n_amt_allowed if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a3

// Provider price variance, allowed amount for procedure
areg iqr_n_amt_allowed_radio web_post i.ym $ctr_vars1, absorb(proc_id_radio) vce(cl ym)
summ iqr_n_amt_allowed_radio if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a4


esttab a1 a2 a3 a4 using $output/price_dispersion_regs_a.tex, frag cells(b(fmt(1) star) se(fmt(1) par)) ///
	keep(web_post )  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
esttab a1 a2 a3 a4 using $output/price_dispersion_regs_b.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(ymeanexp ar2 N, fmt(%6.1fc 3 %12.0fc) labels("Mean IQR" "Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 



